/*
 * Copyright (c) 2008-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.blogclient.options;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.panteleyev.blogclient.Options;
import org.panteleyev.swing.SwingFactory;

/**
 *
 * @author Petr Panteleyev
 */

class ApplicationPanel extends OptionsDialogPanel {
    private AbstractAction      chooseBrowserAction;

    private JTextField          browserCmdEdit;
    private JButton             browserCmdButton;
    private JCheckBox           useExternalPreviewCheck;
    private JComboBox<String>   langList;
    private JCheckBox           showProfileManagerCheck;

    /** Creates new form ApplicationPanel */
    public ApplicationPanel() {
        createActions();
        initComponents();

        if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
            browserCmdEdit.setVisible(false);
            browserCmdButton.setVisible(false);
        }
    }

    private void createActions() {
        chooseBrowserAction = new AbstractAction("...") {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser dial = new JFileChooser();

                dial.setAcceptAllFileFilterUsed(true);
                int ret = dial.showOpenDialog(getParent());

                if (ret == JFileChooser.APPROVE_OPTION) {
                    File f = dial.getSelectedFile();
                    try {
                        String name = f.getCanonicalPath();
                        browserCmdEdit.setText(name);
                    } catch (IOException ex) {

                    }
                }
            }
        };
    }

    private void initComponents() {
        JPanel panel;

        setLayout(new BorderLayout());
        add(panel = SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_Y,
            showProfileManagerCheck = new JCheckBox(L10N.OPTIONS_DIALOG_SHOW_PROFILE_MANAGER.s()),
            Box.createVerticalStrut(5),
            SwingFactory.createPanel(BorderFactory.createTitledBorder(L10N.OPTIONS_DIALOG_EXTERNAL_BROWSER_LABEL.s()), SwingFactory.LAYOUT.BOX_Y,
                SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                    browserCmdEdit = new JTextField(),
                    browserCmdButton = new JButton(chooseBrowserAction)
                ),
                Box.createVerticalStrut(3),
                SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                    useExternalPreviewCheck = new JCheckBox(L10N.OPTIONS_DIALOG_EXTERNAL_PREVIEW_LABEL.s()),
                    Box.createHorizontalGlue()
                )
            ),
            SwingFactory.createPanel(BorderFactory.createEmptyBorder(5, 5, 5, 5), SwingFactory.LAYOUT.BOX_X,
                new JLabel(L10N.OPTIONS_DIALOG_LANGUAGE_LABEL.s()),
                Box.createHorizontalStrut(3),
                langList = new JComboBox<>(new String[] {"System Default", "English", "Russian"}),
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(5),
            new JLabel(L10N.OPTIONS_DIALOG_RESTART_LABEL.s())
        ), BorderLayout.NORTH);

        langList.setMaximumSize(langList.getPreferredSize());
        SwingFactory.setAlignmentX(panel, LEFT_ALIGNMENT);
    }

    @Override
    public void onShow() {
        useExternalPreviewCheck.setSelected(Options.getUseExternalPreview());
        browserCmdEdit.setText(Options.getExternalBrowserCommand());
        langList.setSelectedIndex(Options.getLocaleIndex());
        showProfileManagerCheck.setSelected(Options.getShowProfileManager());
    }

    @Override
    public void onOk() {
        Options.setLocaleIndex(langList.getSelectedIndex());
        Options.setExternalBrowserCommand(browserCmdEdit.getText());
        Options.setUseExternalPreview(useExternalPreviewCheck.isSelected());
        Options.setShowProfileManager(showProfileManagerCheck.isSelected());
    }

}
